#!/usr/env python

import csv
from datetime import datetime

def write_doc_comment(test_file, author, date, case_id, purpose, priority):
    test_file.write("/**\n")
    test_file.write(" * @author: %s\n" % author)
    test_file.write(" * @date: %s\n" % date.isoformat())
    test_file.write(" * @version: 0.1\n")
    test_file.write(" * @TCID: %s\n" % case_id)
    test_file.write(" * Purpose: test %s\n" % purpose)
    test_file.write(" * Priority: %s\n" % priority)
    test_file.write(" */\n")

def alim_test(test_file, test_name, alt_layer_value, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Tcas.Alt_Layer_Value = %s;\n" % alt_layer_value);
    test_file.write("    assertEquals(%s, Tcas.ALIM());\n" % expected);
    test_file.write("}\n\n")

def own_below_threat_test(test_file, test_name, own_tracked_alt, other_tracked_alt, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Tcas.Own_Tracked_Alt = %s;\n" % own_tracked_alt);
    test_file.write("    Tcas.Other_Tracked_Alt = %s;\n" % other_tracked_alt);
    if expected == 'true':
        test_file.write("    assertTrue(Tcas.Own_Below_Threat());\n")
    else:
        test_file.write("    assertFalse(Tcas.Own_Below_Threat());\n")
    test_file.write("}\n\n")

def own_above_threat_test(test_file, test_name, own_tracked_alt, other_tracked_alt, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Tcas.Own_Tracked_Alt = %s;\n" % own_tracked_alt);
    test_file.write("    Tcas.Other_Tracked_Alt = %s;\n" % other_tracked_alt);
    if expected == 'true':
        test_file.write("    assertTrue(Tcas.Own_Above_Threat());\n")
    else:
        test_file.write("    assertFalse(Tcas.Own_Above_Threat());\n")
    test_file.write("}\n\n")

def inhibit_biased_climb_test(test_file, test_name, up_separation, climb_inhibit, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Tcas.Up_Separation = %s;\n" % up_separation);
    test_file.write("    Tcas.Climb_Inhibit = %s;\n" % climb_inhibit);
    test_file.write("    assertEquals(%s, Tcas.Inhibit_Biased_Climb());\n" % expected);
    test_file.write("}\n\n")

def non_crossing_biased_climb_test(test_file, test_name, cur_vertical_sep, own_tracked_alt, other_tracked_alt, alt_layer_value, up_separation, down_separation, climb_inhibit, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Tcas.Cur_Vertical_Sep = %s;\n" % cur_vertical_sep);
    test_file.write("    Tcas.Own_Tracked_Alt = %s;\n" % own_tracked_alt);
    test_file.write("    Tcas.Other_Tracked_Alt = %s;\n" % other_tracked_alt);
    test_file.write("    Tcas.Alt_Layer_Value = %s;\n" % alt_layer_value);
    test_file.write("    Tcas.Up_Separation = %s;\n" % up_separation);
    test_file.write("    Tcas.Down_Separation = %s;\n" % down_separation);
    test_file.write("    Tcas.Climb_Inhibit = %s;\n" % climb_inhibit);
    if expected == 'true':
        test_file.write("    assertTrue(Tcas.Non_Crossing_Biased_Climb());\n");
    else:
        test_file.write("    assertFalse(Tcas.Non_Crossing_Biased_Climb());\n");
    test_file.write("}\n\n")

def non_crossing_biased_descend_test(test_file, test_name, cur_vertical_sep, own_tracked_alt, other_tracked_alt, alt_layer_value, up_separation, down_separation, climb_inhibit, expected):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Tcas.Cur_Vertical_Sep = %s;\n" % cur_vertical_sep);
    test_file.write("    Tcas.Own_Tracked_Alt = %s;\n" % own_tracked_alt);
    test_file.write("    Tcas.Other_Tracked_Alt = %s;\n" % other_tracked_alt);
    test_file.write("    Tcas.Alt_Layer_Value = %s;\n" % alt_layer_value);
    test_file.write("    Tcas.Up_Separation = %s;\n" % up_separation);
    test_file.write("    Tcas.Down_Separation = %s;\n" % down_separation);
    test_file.write("    Tcas.Climb_Inhibit = %s;\n" % climb_inhibit);
    if expected == 'true':
        test_file.write("    assertTrue(Tcas.Non_Crossing_Biased_Descend());\n");
    else:
        test_file.write("    assertFalse(Tcas.Non_Crossing_Biased_Descend());\n");
    test_file.write("}\n\n")

def alt_sep_test(test_file, test_name, alt_layer_value, own_tracked_alt, other_tracked_alt, up_separation, climb_inhibit, cur_vertical_sep, down_separation, high_confidence, two_of_three_reports_valid, own_tracked_alt_rate, other_rac, other_capability):
    test_file.write("@Test public void %s() {\n" % test_name)
    test_file.write("    Tcas.Alt_Layer_Value = %s;\n" % alt_layer_value);
    test_file.write("    Tcas.Own_Tracked_Alt = %s;\n" % own_tracked_alt);
    test_file.write("    Tcas.Other_Tracked_Alt = %s;\n" % other_tracked_alt);
    test_file.write("    Tcas.Up_Separation = %s;\n" % up_separation);
    test_file.write("    Tcas.Climb_Inhibit = %s;\n" % climb_inhibit);
    test_file.write("    Tcas.Cur_Vertical_Sep = %s;\n" % cur_vertical_sep);
    test_file.write("    Tcas.Down_Separation = %s;\n" % down_separation);
    test_file.write("    Tcas.High_Confidence = %s;\n" % high_confidence);
    test_file.write("    Tcas.Two_of_Three_Reports_Valid = %s;\n" % two_of_three_reports_valid);
    test_file.write("    Tcas.Own_Tracked_Alt_Rate = %s;\n" % own_tracked_alt_rate);
    test_file.write("    Tcas.Other_RAC = %s;\n" % other_rac);
    test_file.write("    Tcas.Other_Capability = %s;\n" % other_capability);
    test_file.write("    assertEquals(Tcas.%s, Tcas.alt_sep_test());\n" % expected)
    test_file.write("}\n\n")

with open('tcas_test_case_design.csv') as csv_file:
    reader = csv.reader(csv_file)
    reader.next() # skip title line
    
    header = reader.next()
    case_idx = header.index('Test Case')
    name_idx = header.index('Name')

    #variables
    alv_idx = header.index('Alt_Layer_Value')
    owta_idx = header.index('Own_Tracked_Alt')
    otta_idx = header.index('Other_Tracked_Alt')
    usep_idx = header.index('Up_Separation')
    cinh_idx = header.index('Climb_Inhibit')

    cvsp_idx = header.index('Cur_Vertical_Sep')
    dsep_idx = header.index('Down_Separation')

    hcon_idx = header.index('High_Confidence')
    ttrv_idx = header.index('Two_of_Three_Reports_Valid')
    otar_idx = header.index('Own_Tracked_Alt_Rate')
    orac_index = header.index('Other_RAC')
    ocap_idx = header.index('Other_Capability')

    exp_idx = header.index('Expected')
    pps_idx = header.index('Purpose')
    prt_idx = header.index('Priority')
    

    today = datetime.today()
    with open('tcas_test_methods.java', 'w') as test_file:
        for row in reader:
            test_case = row[case_idx]
            test_name = row[name_idx]
            purpose = row[pps_idx]
            priority = row[prt_idx]

            alt_layer_value = row[alv_idx]
            own_tracked_alt = row[owta_idx]
            other_tracked_alt = row[otta_idx]
            up_separation = row[usep_idx]
            climb_inhibit = row[cinh_idx]
            cur_vertical_sep = row[cvsp_idx]
            down_separation = row[dsep_idx]

            high_confidence = row[hcon_idx]
            two_of_three_reports_valid = row[ttrv_idx]
            own_tracked_alt_rate = row[otar_idx]
            other_rac = row[orac_index]
            other_capability = row[ocap_idx]
            expected = row[exp_idx]

            write_doc_comment(test_file, "kavinyao", today, test_case, purpose, priority)
            if "ALIMTest" in test_name:
                alim_test(test_file, test_name, alt_layer_value, expected)
            elif "Own_Below_ThreatTest" in test_name:
                own_below_threat_test(test_file, test_name, own_tracked_alt, other_tracked_alt, expected)
            elif "Own_Above_ThreatTest" in test_name:
                own_above_threat_test(test_file, test_name, own_tracked_alt, other_tracked_alt, expected)
            elif "Inhibit_Biased_ClimbTest" in test_name:
                inhibit_biased_climb_test(test_file, test_name, up_separation, climb_inhibit, expected)
            elif "Non_Crossing_Biased_ClimbTest" in test_name:
                non_crossing_biased_climb_test(test_file, test_name, cur_vertical_sep, own_tracked_alt, other_tracked_alt, alt_layer_value, up_separation, down_separation, climb_inhibit, expected)
            elif "Non_Crossing_Biased_DescendTest" in test_name:
                non_crossing_biased_descend_test(test_file, test_name, cur_vertical_sep, own_tracked_alt, other_tracked_alt, alt_layer_value, up_separation, down_separation, climb_inhibit, expected)
            elif "alt_sep_test" in test_name:
                alt_sep_test(test_file, test_name, alt_layer_value, own_tracked_alt, other_tracked_alt, up_separation, climb_inhibit, cur_vertical_sep, down_separation, high_confidence, two_of_three_reports_valid, own_tracked_alt_rate, other_rac, other_capability)

